package com.coldwindblows.blog.bo;

/**
 * rest返回对象
 * Created by ColdWindBlows on 2018/07/01
 *
 * @param <T>
 */
@SuppressWarnings("rawtypes")
public class RestResponseBo<T> {
	/**
	 * 服务器响应数据
	 */
	private T payload;
	/**
	 * 请求是否成功
	 */
	private boolean success;
	/**
	 * 错误信息
	 */
	private String msg;
	/**
	 * 状态码
	 */
	private int code = -1;
	/**
	 * 服务器响应时间
	 */
	private long timestamp;
	
	public RestResponseBo() {
		this.timestamp = System.currentTimeMillis() / 1000;
	}
	
	public RestResponseBo(boolean success) {
		this.timestamp = System.currentTimeMillis() / 1000;
		this.success = success;
	}
	
	public RestResponseBo(boolean success, T payload) {
		this.timestamp = System.currentTimeMillis() / 1000;
		this.success = success;
		this.payload = payload;
	}
	
	public RestResponseBo(boolean success, String msg) {
		this.timestamp = System.currentTimeMillis() / 1000;
		this.success = success;
		this.msg = msg;
	}
	
	public RestResponseBo(boolean success, T payload, int code) {
		this.timestamp = System.currentTimeMillis() / 1000;
		this.success = success;
		this.payload = payload;
		this.code = code;
	}

	public T getPayload() {
		return payload;
	}

	public void setPayload(T payload) {
		this.payload = payload;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}
	
	public static RestResponseBo ok() {
		return new RestResponseBo<>(true);
	}
	
	public static <T> RestResponseBo ok(T payload) {
		return new RestResponseBo<>(true, payload);
	}
	
	public static <T> RestResponseBo ok(int code) {
		return new RestResponseBo<>(true, null, code);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> RestResponseBo ok(T payload, int code) {
		return new RestResponseBo(true, payload, code);
	}
	
	public static RestResponseBo fail() {
		return new RestResponseBo<>(false);
	}
	
	public static RestResponseBo fail(String msg) {
		return new RestResponseBo(false, msg);
	}
	
	@SuppressWarnings("unchecked")
	public static RestResponseBo fail(int code) {
		return new RestResponseBo(false, null, code);
	}
	
	@SuppressWarnings("unchecked")
	public static RestResponseBo fail(int code, String msg) {
		return new RestResponseBo(false, msg, code);
	}
	
}
